home *** CD-ROM | disk | FTP | other *** search
/ Total Network Tools 2002 / NextStepPublishing-TotalNetworkTools2002-Win95.iso / Archive / Misc Servers / Zope.exe / DBI_DB.PY < prev    next >
Encoding:
Python Source  |  2000-05-30  |  8.1 KB  |  223 lines

  1. ##############################################################################
  2. # Zope Public License (ZPL) Version 1.0
  3. # -------------------------------------
  4. # Copyright (c) Digital Creations.  All rights reserved.
  5. # This license has been certified as Open Source(tm).
  6. # Redistribution and use in source and binary forms, with or without
  7. # modification, are permitted provided that the following conditions are
  8. # met:
  9. # 1. Redistributions in source code must retain the above copyright
  10. #    notice, this list of conditions, and the following disclaimer.
  11. # 2. Redistributions in binary form must reproduce the above copyright
  12. #    notice, this list of conditions, and the following disclaimer in
  13. #    the documentation and/or other materials provided with the
  14. #    distribution.
  15. # 3. Digital Creations requests that attribution be given to Zope
  16. #    in any manner possible. Zope includes a "Powered by Zope"
  17. #    button that is installed by default. While it is not a license
  18. #    violation to remove this button, it is requested that the
  19. #    attribution remain. A significant investment has been put
  20. #    into Zope, and this effort will continue if the Zope community
  21. #    continues to grow. This is one way to assure that growth.
  22. # 4. All advertising materials and documentation mentioning
  23. #    features derived from or use of this software must display
  24. #    the following acknowledgement:
  25. #      "This product includes software developed by Digital Creations
  26. #      for use in the Z Object Publishing Environment
  27. #      (http://www.zope.org/)."
  28. #    In the event that the product being advertised includes an
  29. #    intact Zope distribution (with copyright and license included)
  30. #    then this clause is waived.
  31. # 5. Names associated with Zope or Digital Creations must not be used to
  32. #    endorse or promote products derived from this software without
  33. #    prior written permission from Digital Creations.
  34. # 6. Modified redistributions of any form whatsoever must retain
  35. #    the following acknowledgment:
  36. #      "This product includes software developed by Digital Creations
  37. #      for use in the Z Object Publishing Environment
  38. #      (http://www.zope.org/)."
  39. #    Intact (re-)distributions of any official Zope release do not
  40. #    require an external acknowledgement.
  41. # 7. Modifications are encouraged but must be packaged separately as
  42. #    patches to official Zope releases.  Distributions that do not
  43. #    clearly separate the patches from the original work must be clearly
  44. #    labeled as unofficial distributions.  Modifications which do not
  45. #    carry the name Zope may be packaged in any form, as long as they
  46. #    conform to all of the clauses above.
  47. # Disclaimer
  48. #   THIS SOFTWARE IS PROVIDED BY DIGITAL CREATIONS ``AS IS'' AND ANY
  49. #   EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  50. #   IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  51. #   PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL DIGITAL CREATIONS OR ITS
  52. #   CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  53. #   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  54. #   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  55. #   USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  56. #   ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  57. #   OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  58. #   OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  59. #   SUCH DAMAGE.
  60. # This software consists of contributions made by Digital Creations and
  61. # many individuals on behalf of Digital Creations.  Specific
  62. # attributions are listed in the accompanying credits file.
  63. ##############################################################################
  64. __doc__='''short description
  65.  
  66.  
  67. $Id: dbi_db.py,v 1.8 2000/05/30 15:46:55 brian Exp $'''
  68. #     Copyright 
  69. #
  70. #       Copyright 1997 Digital Creations, Inc, 910 Princess Anne
  71. #       Street, Suite 300, Fredericksburg, Virginia 22401 U.S.A. All
  72. #       rights reserved. 
  73. __version__='$Revision: 1.8 $'[11:-2]
  74.  
  75. import string, sys
  76. from string import strip, split, find, join
  77.  
  78. failures=0
  79. calls=0
  80.  
  81. nonselect_desc=[
  82.     ('Query',  'STRING', 62, 62, 0, 0, 1),
  83.     ('Status', 'STRING', 12, 12, 0, 0, 1),
  84.     ('Calls',  'STRING', 12, 12, 0, 0, 1),
  85.     ]
  86.  
  87. class DB:
  88.  
  89.     _p_oid=_p_changed=_registered=None
  90.  
  91.     defs={'STRING':'s', 'NUMBER':'n', 'DATE':'d'}
  92.  
  93.     def Database_Connection(self, string):
  94.         # Create a dbi-compatible database connection
  95.         raise 'ImplementedBySubclass', (
  96.             'attempt to create a database connection for an abstract dbi')
  97.  
  98.     Database_Error='Should be overriden by subclass'
  99.  
  100.     def __init__(self,connection):
  101.         self.connection=connection
  102.         db=self.db=self.Database_Connection(connection)
  103.         self.cursor=db.cursor()
  104.         
  105.     def str(self,v, StringType=type('')):
  106.         if v is None: return ''
  107.         r=str(v)
  108.         if r[-1:]=='L' and type(v) is not StringType: r=r[:-1]
  109.         return r
  110.  
  111.     def __inform_commit__(self, *ignored):
  112.         self._registered=None
  113.         self.db.commit()
  114.  
  115.     def __inform_abort__(self, *ignored):
  116.         self._registered=None
  117.         self.db.rollback()
  118.  
  119.     def register(self):
  120.         if self._registered: return
  121.         get_transaction().register(self)
  122.         self._registered=1
  123.         
  124.  
  125.     def query(self,query_string, max_rows=9999999):
  126.         global failures, calls
  127.         calls=calls+1
  128.         try:
  129.             c=self.cursor
  130.             self.register()
  131.             queries=filter(None, map(strip,split(query_string, '\0')))
  132.             if not queries: raise 'Query Error', 'empty query'
  133.             if len(queries) > 1:
  134.                 result=[]
  135.                 for qs in queries:
  136.                     r=c.execute(qs)
  137.                     if r is None: raise 'Query Error', (
  138.                         'select in multiple sql-statement query'
  139.                         )
  140.                     result.append((qs, str(`r`), calls))
  141.                 desc=nonselect_desc
  142.             else:
  143.                 query_string=queries[0]
  144.                 r=c.execute(query_string)
  145.                 if r is None:
  146.                     result=c.fetchmany(max_rows)
  147.                     desc=c.description
  148.                 else:
  149.                     result=((query_string, str(`r`), calls),)
  150.                     desc=nonselect_desc
  151.             failures=0
  152.             c.close()
  153.         except self.Database_Error, mess:
  154.             c.close()
  155.             self.db.rollback()
  156.             failures=failures+1
  157.             if ((find(mess,": invalid") < 0 and
  158.                  find(mess,"PARSE") < 0) or
  159.                 # DBI IS stupid
  160.                 find(mess,
  161.                      "Error while trying to retrieve text for error") > 0
  162.                 or
  163.                 # If we have a large number of consecutive failures,
  164.                 # our connection is probably dead.
  165.                 failures > 100
  166.                 ):
  167.                 # Hm. maybe the db is hosed.  Let's try once to restart it.
  168.                 failures=0
  169.                 c.close()
  170.                 self.db.close()
  171.                 db=self.db=self.Database_Connection(self.connection)
  172.                 self.cursor=db.cursor()
  173.                 c=self.cursor
  174.                 c.execute(query_string)
  175.                 result=c.fetchall()
  176.                 desc=c.description
  177.             else:
  178.                 raise sys.exc_info()
  179.         
  180.         if result:
  181.             result=join(
  182.                     map(
  183.                         lambda row, self=self:
  184.                         join(map(self.str,row),'\t'),
  185.                         result),
  186.                     '\n')+'\n'
  187.         else:
  188.             result=''
  189.  
  190.         return (
  191.             "%s\n%s\n%s" % (
  192.                 join(map(lambda d: d[0],desc), '\t'),
  193.                 join(
  194.                     map(
  195.                         lambda d, defs=self.defs: "%d%s" % (d[2],defs[d[1]]),
  196.                         desc),
  197.                     '\t'),
  198.                 result,
  199.                 )
  200.             )
  201.